<ol class="breadcrumb">
    <li><a href="#">一元中奖</a></li>
    <li>活动管理</li>
</ol>
<div class="panel panel-default">
    <div class="panel-heading">
        <span class="icon-gift icon-large"></span>
        <span class="panel-title">活动列表</span>
    </div>
    <div class="panel-body">
        <% if(session.permissionMap[3003]){ %>
        <a href="/activity/add" class="btn btn-success btn-sm">新增</a>
        <% } %>
        <% if(session.permissionMap[3005]){ %>
        <a href="#" onclick="getCheckIds()" class="btn btn-primary btn-sm">编辑</a>
        <% } %>
    </div>
    <div class="table-responsive">
        <table class="table table-hover table-striped" id="activityTable">
            <thead>
            <tr>
                <th>√</th>
                <th>预览</th>
                <th>活动名称</th>
                <th>活动描述</th>
                <th>活动链接</th>
                <th>创建时间</th>
            </tr>
            </thead>
            <tfoot>
            <tr>
                <td colspan="6">
                    <nav class="twbsPagination"></nav>
                </td>
            </tr>
            </tfoot>
            <tbody>
            <% data.rows.forEach(function(activity, index){ %>
            <tr>
                <td><input name="activity_id" type="checkbox" value="<%= activity.id %>"></td>
                <td>
                    <div class="thumb" style="background-image: url('<%= activity.thumb %>');"></div>
                </td>
                <td><%= activity.name %></td>
                <td><%= activity.description %></td>
                <td><%= activity.url %></td>
                <td><%= activity.createtime %></td>
            </tr>
            <% }) %>

            </tbody>
        </table>
    </div>
</div>
<style>
    .thumb {
        background-repeat: no-repeat;
        background-size: cover;
        height: 50px;
        width: 50px;
        margin: 3px;
    }
</style>
<script>
    init(<%= data.total%>);//初始化分页
    function init(total) {
        //分页功能
        $('.twbsPagination').twbsPagination({
            totalPages: parseInt((Number(total) + 10 - 1) / 10, 10),
            visiblePages: 10,
            onPageClick: function (event, page) {
                reloadpage(page, false);
            }
        });
    }
    function reloadpage(page, isinit) {
        $.get("/activity/list",
                {page: page},
                function (result) {
                    if (isinit) {
                        init(result.total);
                    }

                    if (result.rows.length < 1) {
                        $('#rowcontent').html('无数据...');
                        return;
                    }
                    var html = '';
                    result.rows.forEach(function (activity, index) {
                        html += '<tr>' +
                                '<td><input name="activity_id" type="checkbox" value="' + activity.id + '"></td>' +
                                '<td><div class="thumb" style="background-image: url(\'' + activity.thumb + '\');"></div></td>' +
                                '<td>' + activity.name + '</td>' +
                                '<td>' + activity.description + '</td>' +
                                '<td>' + activity.url + '</td> ' +
                                '<td>' + activity.createtime + '</td>' +
                                '</tr>';
                    });
                    $('#activityTable tbody').html(html);
                });
    }


    function getCheckIds() {
        var adIds = "";
        $("input:checkbox[name='activity_id']:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            }
        });
        if (!adIds) {
            return alert('请选择一件商品!');
        }
        window.location.href = "/activity/edit?id=" + adIds;
    }
    function getCheckIds1() {
        var adIds = "";
        $("input:checkbox[name='activity_id']:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
//            alert(adIds);
        });

    }
</script>